<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Product extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
    function __construct()
    {
        parent::__construct();
    	$this->load->library('session');
    	$this->load->database();
    	$this->load->model("modclient");
    	$this->load->helper('url');
    	$this->load->helper('language');
    	$this->load->helper('imagemerge');
    	$this->load->library('email');
    	$this->lang->load('pages');
    	$this->load->library('cart');
    	
	}
	

	
	    
	public function mainpage($page=0)
		{
					
			//$data["listproduct"]=$this->modclient->getlistproducts();
			//pagination
			$this->load->library('pagination');
			$config['base_url'] = base_url()."client/product/mainpage/";
			$config['total_rows'] = $this->db->get("products")->num_rows();
			$config['per_page'] = 30;
			$config['num_links'] = 3;
			$config['first_link'] = '&laquo;';
			$config['last_link'] = '&raquo;';
			$config['num_tag_open']="<li>";
			$config['num_tag_close']="</li>";
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';
			$config['cur_tag_open']="<li class='active'><a href='#'>";
			$config['cur_tag_close']="</a></li>";
			$this->pagination->cur_page=$this->uri->segment(4);
			$this->pagination->initialize($config);
			$data["listproduct"]=$this->modclient->getlistproductbypage($config['per_page'],$this->uri->segment(4));
			$data["link"]=$this->pagination->create_links();
			$data["lang"]=$this->config->item('language');
			$data["sectorname"]="produits/products/商品";
			
			$this->load->view("client/mainpage",$data);
			 
		}
	
		
		
public function  productsbycategories($cats)
{
	
	$cats_final=str_replace("-"," ",$cats);
	$this->load->library('pagination');
	$config['base_url'] = base_url()."client/product/productsbycategories/$cats/";
	$config['total_rows'] = $this->db->get_where("products",array('Catalog' => $cats_final))->num_rows();
	$config['per_page'] = 30;
	$config['num_links'] = 3;
	$config['first_link'] = '&laquo;';
	$config['last_link'] = '&raquo;';
	$config['num_tag_open']="<li>";
	$config['num_tag_close']="</li>";
	$config['last_tag_open'] = '<li>';
	$config['last_tag_close'] = '</li>';
	$config['first_tag_open'] = '<li>';
	$config['first_tag_close'] = '</li>';
	$config['next_tag_open'] = '<li>';
	$config['next_tag_close'] = '</li>';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['cur_tag_open']="<li class='active'><a href='#'>";
	$config['cur_tag_close']="</a></li>";
	$this->pagination->cur_page=$this->uri->segment(5);
	$this->pagination->initialize($config);
	$data["listproduct"]=$this->modclient->getlistproductbycats($cats_final,$config['per_page'],$this->uri->segment(5));
	$data["link"]=$this->pagination->create_links();
	$data["lang"]=$this->config->item('language');
	$query_current_cat=$this->db->get_where("categories",array("catecode"=>$cats));
	$sector_arr=$query_current_cat->result_array();
	$data["sectorname"]=$sector_arr[0]["descptions"];
	$this->load->view("client/mainpage",$data);
	
}		


/*
public function filtrebycountry($cats="all",$pays)
{
	$cats_final=str_replace("-"," ",$cats);
	$this->load->library('pagination');
	$config['base_url'] = base_url()."client/product/filtrebycountry/$cats/$pays/";
	if($cats=="all")
	{
		$this->db->like('LOWER(Comment)', strtolower($pays));
		$ojbetproduct=$this->db->get("products");
		$data["listproduct"]=$this->modclient->getlistproductbypaysfiltre($cats_final,$pays,$config['per_page'],$this->uri->segment(6));
		
	}else 
	{
		$cats_final=str_replace("-"," ",$cats);
		$this->db->like('LOWER(Comment)', strtolower($pays));
		$ojbetproduct=$this->db->get_where("products",array('Catalog' => $cats_final));
		$data["listproduct"]=$this->modclient->getlistproductbypaysfiltre($cats_final,$pays,$config['per_page'],$this->uri->segment(6));
	}	
	
	$config['total_rows'] = $ojbetproduct->num_rows();
	$config['per_page'] = 30;
	$config['num_links'] = 3;
	$config['first_link'] = '&laquo;';
	$config['last_link'] = '&raquo;';
	$config['num_tag_open']="<li>";
	$config['num_tag_close']="</li>";
	$config['last_tag_open'] = '<li>';
	$config['last_tag_close'] = '</li>';
	$config['first_tag_open'] = '<li>';
	$config['first_tag_close'] = '</li>';
	$config['next_tag_open'] = '<li>';
	$config['next_tag_close'] = '</li>';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['cur_tag_open']="<li class='active'><a href='#'>";
	$config['cur_tag_close']="</a></li>";
	$this->pagination->cur_page=$this->uri->segment(6);
	$this->pagination->initialize($config);
	$data["link"]=$this->pagination->create_links();
	$data["lang"]=$this->config->item('language');
	$data["cats"]=$cats;
	$this->load->view("client/mainpage",$data);
	
}
*/


public function recherche()
{
	$key=$this->input->get_post("key");
	$this->load->library('pagination');
	$config['base_url'] = base_url()."client/product/recherche2/$key/";
		$this->db->like('LOWER(description)', strtolower($key));
		$this->db->or_like('LOWER(Comment)', strtolower($key));
		$this->db->or_like('LOWER(ItemCode)', strtolower($key));
		$ojbetproduct=$this->db->get("products");
		$config['total_rows'] = $ojbetproduct->num_rows();
		$config['per_page'] = 30;
		$config['num_links'] = 3;
		$config['first_link'] = '&laquo;';
		$config['last_link'] = '&raquo;';
		$config['num_tag_open']="<li>";
		$config['num_tag_close']="</li>";
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['cur_tag_open']="<li class='active'><a href='#'>";
		$config['cur_tag_close']="</a></li>";
		$this->pagination->cur_page=$this->uri->segment(5);
		$this->pagination->initialize($config);
		$data["listproduct"]=$this->modclient->recherche($key,$config['per_page'],$this->uri->segment(5));
		$data["link"]=$this->pagination->create_links();
		$data["lang"]=$this->config->item('language');
		$data["sectorname"]=$key;
		$this->load->view("client/mainpage",$data);

}

public function recherche2($key)
{
	
	$this->load->library('pagination');
	$config['base_url'] = base_url()."client/product/recherche2/$key/";
	$this->db->like('LOWER(description)', strtolower($key));
	$this->db->or_like('LOWER(Comment)', strtolower($key));
	$this->db->or_like('LOWER(ItemCode)', strtolower($key));
	$ojbetproduct=$this->db->get("products");
	$config['total_rows'] = $ojbetproduct->num_rows();
	$config['per_page'] = 30;
	$config['num_links'] = 3;
	$config['first_link'] = '&laquo;';
	$config['last_link'] = '&raquo;';
	$config['num_tag_open']="<li>";
	$config['num_tag_close']="</li>";
	$config['last_tag_open'] = '<li>';
	$config['last_tag_close'] = '</li>';
	$config['first_tag_open'] = '<li>';
	$config['first_tag_close'] = '</li>';
	$config['next_tag_open'] = '<li>';
	$config['next_tag_close'] = '</li>';
	$config['prev_tag_open'] = '<li>';
	$config['prev_tag_close'] = '</li>';
	$config['cur_tag_open']="<li class='active'><a href='#'>";
	$config['cur_tag_close']="</a></li>";
	$this->pagination->cur_page=$this->uri->segment(5);
	$this->pagination->initialize($config);
	$data["listproduct"]=$this->modclient->recherche($key,$config['per_page'],$this->uri->segment(5));
	$data["link"]=$this->pagination->create_links();
	$data["lang"]=$this->config->item('language');
	$data["sectorname"]=$key;
	$this->load->view("client/mainpage",$data);

}

public function productdetail()
{
	$this->load->view("client/productdetail");
	
}



		
//shoping cart sector	
		
		public function findcart($idproducts)
		{
			$reslt=false;
			$qty=0;
			foreach ($this->cart->contents() as $items)
			{
				if($items["id"]==$idproducts)
				{
					$reslt= $items["rowid"];
					$qty=$items["qty"];
					break;
				}
			}
		
		
			if($reslt!=false)
			{
				$this->updatcar($reslt, $qty);
					
			}else
			{
					
				$this->addcart($idproducts);
				
					
			}
			//print_r($this->cart->contents());
			//$this->cart->destroy();
			//return item quantity
			$total_qty=0;
			foreach ($this->cart->contents() as $c)
				{
					$total_qty+=$c["qty"];
				}
				echo $total_qty;
		
		}
		
		public  function addcart($idproducts)
		{
		
			$arr_products=$this->modclient->findproductbyid($idproducts);
			$data = array(
					'id'      => $arr_products[0]['idproducts'],
					'qty'     => 1,
					'price'   => $arr_products[0]['price'],
					'name'    => $arr_products[0]['ItemCode'],
					'options' => array('comment' => $arr_products[0]["Comment"], 'desc' => $arr_products[0]['Description'],"barcode"=>$arr_products[0]['Barcode'])
					//'itemcode'=>$arr_products[0]["ItemCode"],
					//'desc'=>$arr_products[0]["Comment"],
					//'barcode'=>$arr_products[0]["Barcode"]
		
			);
						
			$this->cart->insert($data);
					
		}
		
		public  function updatcar($idrow,$qty)
		{
		
			$data = array(
					'rowid' => $idrow,
					'qty'   => $qty+1,
			);
		
			$this->cart->update($data);
			
		}
		
		public function updateshopcart()
		{
			$idrow=$this->input->get_post("idrow");
			$qty=$this->input->get_post("qty");
			$data = array(
					'rowid' => $idrow,
					'qty'   => $qty,
					);
			
			$this->cart->update($data);
			$items=$this->cart->contents();
			if(array_key_exists($idrow,$items)){
			$return_val["total"]=$this->cart->total();
			$return_val["subtotal"]=$items[$idrow]["subtotal"];
			echo json_encode($return_val);
			}else 
			{
				echo false;
			}
		}
		
		public function destroy()
		{
			$this->cart->destroy();
		}
		
	
}


?>
